﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class DomainBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDomainDA domainDA = DataAccess.CreateDomainDA();
        private PubParasInfo pubParasInfo;

        public DomainBLL()
        {
            this.pubParasInfo = null;
            this.dataBaseID = new List<string>();
            this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
        }

        public DomainBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DomainBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> domainIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<DomainInfo> domainInfos = new List<DomainInfo>();
                DomainInfo domainInfo = null;
                foreach (string id in domainIDS)
                {
                    domainInfo = domainDA.GetDomainByID(dataBase, tran, id, this.dataBaseID[0]);
                    domainInfos.Add(domainInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (domainDA.DeleteDomain(dataBase, tran, domainIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < domainIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<DomainInfo>(dataBase, tran, domainInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DOMAIN", this.dataBaseID);
                        temp = temp + domainIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return domainDA.GetAllDomain(this.dataBaseID[0]);
        }

        public object GetInfoByID(string domainID)
        {
            return domainDA.GetDomainByID(domainID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            throw new NotImplementedException();
        }

        public string Insert(object obj)
        {
            string DomainID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DomainInfo domain = obj as DomainInfo;
                if (domain.DomainID == 0)
                {
                    domain.DomainID = Convert.ToInt32(commonMethod.GenNextNumStr(dataBase, tran, "Domain", this.dataBaseID[0]));
                }
                if (domainDA.CheckDomainIDUnique(dataBase, tran, domain.DomainID.ToString(), this.dataBaseID[0]))
                {
                    throw new Exception("仓库'" + domain.DomainID + "'已经存在! 请重新输入!");
                }
                domain.CreatedDate = DateTime.Now;
                domain.UpdatedDate = DateTime.Now;
                domain.UpdatedBy = domain.CreatedBy;
                if (domainDA.InsertDomain(domain, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DomainInfo>(dataBase, tran, null, domain, AuditAction.Insert, this.pubParasInfo.UserID, "DOMAIN", this.dataBaseID);
                    DomainID = domain.DomainID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return DomainID;
        }

        public string Update(object obj)
        {
            string domainID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DomainInfo domain = obj as DomainInfo;
                domain.UpdatedDate = DateTime.Now;
                DomainInfo oldDomainInfo = domainDA.GetDomainByID(dataBase, tran, domain.DomainID.ToString(), this.dataBaseID[0]);
                if (domainDA.UpdateDomain(dataBase, tran, domain, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DomainInfo>(dataBase, tran, oldDomainInfo, domain, AuditAction.Update, this.pubParasInfo.UserID, "DOMAIN", this.dataBaseID);
                    domainID = domain.DomainID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return domainID;
        }
    }
}

